package com.web44.ssm.web;

import com.web44.ssm.utils.ResponseVo;
import jakarta.servlet.http.HttpServletRequest;
import net.sf.jsqlparser.schema.MultiPartName;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.UUID;

/**
 * @Title: UploadController
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2024/1/16 11:13
 */
//@RestController
//@RequestMapping("upload")
public class UploadController {

    @PostMapping("face")
    public ResponseVo uploadWeb(@RequestParam("face") MultipartFile multipartFile
    , HttpServletRequest request) throws IOException {

        String originalFilename = multipartFile.getOriginalFilename();
        String extension = FilenameUtils.getExtension(originalFilename);
        String fileName = UUID.randomUUID().toString().replaceAll("-","");
        String myFileName = fileName +"."+extension;
        // String root = UploadController.class.getResource("/upload/"+myFileName).getPath();
        String root = request.getServletContext().getRealPath("/images/").toString();
        System.out.println(root);
        File file = new File(root+myFileName);
        multipartFile.transferTo(file);
        return ResponseVo.ok("上传成功!","/images/"+myFileName);
    }
}
